#include "CmnServer.h"
CServer::CServer(void)
{
}
bool CServer::Registration(string name_,string password_){
	if(check_first_time(name_)){
		_user_[name_]=password_;
		return true;
	}
	else return false;
}
void CServer::parse(__int8 *recv, std::string &ID,std::string & second)
{
	__int8 delim[]=" ", *temp;
	strtok_s(recv, delim, &temp);
	ID=strtok_s(0, delim, &temp),second =strtok_s(0,delim, &temp);
}
void CServer::parse1(__int8 *recv, std::string &ID)
{
	__int8 delim[]=" ", *temp;
	strtok_s(recv, delim, &temp);
	ID=strtok_s(0, delim, &temp);
}
/*void CServer::parse_ID_password(__int8 *recv, std::string &ID, std::string &password)
{
	__int8 delim[]=" ", *temp;
	strtok_s(recv, delim, &temp);
	ID=strtok_s(0, delim, &temp), password=strtok_s(0, delim, &temp);
}*/
void CServer::DealWith(char* msg, CSocket& X){
	string tempId,tempPass,tempfriend;
	using namespace serverCode;
	switch(*msg)
	{
	case SIGN_UP:
		parse(msg,tempId,tempPass);
		if(Registration(tempId,tempPass))X.Send("DONE",5);
		else X.Send("ERROR",6);
		break;
	case SIGN_IN:
		parse(msg,tempId,tempPass);
		if(_user_[tempId].sign_in(tempPass)){
			string temp_str;temp_str.push_back(WELCOME);
			temp_str+=" "+tempPass;
			X.Send(send(temp_str),strlen(send(temp_str))+1);
		}
		else X.Send("ERROR",6);
		break;
	case SIGN_OUT:
		parse(msg,tempId,tempPass);
		if(_user_[tempId].sign_out(tempPass))X.Send("DONE",5);
		else  X.Send("ERROR",6);
		break;
	case Delete_Account:
		parse(msg,tempId,tempPass);
		if(_user_[tempId].sign_out(tempPass))
			{
				X.Send("DONE",5);
				_user_.erase (tempId);
		    }
		else  X.Send("ERROR",6);
		break;
	case ADD_FRIEND:
		parse(msg,tempId,tempfriend);
		if(IsUser(tempfriend)&&tempfriend!=tempId)
			if(_user_[tempId].ADD_FRIEND(tempId,tempfriend,friend_list,requests))X.Send("DONE",5);
		else  X.Send("ERROR",6);
		break;
	case REMOVE_FRIEND:
		parse(msg,tempId,tempfriend);
		if(IsUser(tempfriend)&&tempfriend!=tempId)
			if(_user_[tempId].REMOVE_FRIEND(tempId,tempfriend,friend_list,requests))X.Send("DONE",5);
		else  X.Send("ERROR",6);
		break;
	case SHOW_REQUEST:
		{
			parse1(msg,tempId);
			string buffer;
			if(_user_[tempId].SHOW_REQUEST(tempId,buffer,requests))
			X.Send(send(buffer),strlen(send(buffer))+1);
			else X.Send("ERROR",6);
		}
		break;
	case CANCEL_REQUEST:
		parse(msg,tempId,tempfriend);
		if(IsUser(tempfriend)&&tempfriend!=tempId)
			if(_user_[tempId].CANCEL_REQUEST(tempId,tempfriend,friend_list,requests))X.Send("DONE",5);
		else  X.Send("ERROR",6);
		break;
	case SHOW_FRIEND:
		{
			parse1(msg,tempId);
			string buffer;
			if(_user_[tempId].SHOW_FRIEND(tempId,buffer,friend_list))
			X.Send(send(buffer),strlen(send(buffer))+1);
			else X.Send("ERROR",6);
		}
		break;
//------------------Amr shibiny------------------------beginning
	case ChangeEnd:
		{
			string event_name;
			char* context;
			strtok_s(msg, " ", &context);
			int hour, minute, day, month, year;
			string _user_name_ = strtok_s(0, " ", &context);
			event_name = strtok_s(0, " ", &context);
			hour = stoi(strtok_s(0, " ", &context));
			minute = stoi(strtok_s(0, " ", &context));
			day = stoi(strtok_s(0, " ", &context));
			month = stoi(strtok_s(0, " ", &context));
			year = stoi(strtok_s(0, " ", &context));
			_user_[_user_name_].edit_start((char*)event_name.c_str(), hour, minute, day, month, year);
			X.Send("Done", 5);
		}break;
	case ChangeBegin:
		{
			string event_name;
			char* context;
			strtok_s(msg, " ", &context);
			int hour, minute, day, month, year;
			string _user_name_ = strtok_s(0, " ", &context);
			event_name = strtok_s(0, " ", &context);
			hour = stoi(strtok_s(0, " ", &context));
			minute = stoi(strtok_s(0, " ", &context));
			day = stoi(strtok_s(0, " ", &context));
			month = stoi(strtok_s(0, " ", &context));
			year = stoi(strtok_s(0, " ", &context));
			_user_[_user_name_].edit_start((char*)event_name.c_str(), hour, minute, day, month, year);
			X.Send("Done", 5);
		}break;
	case AddEvent : 
		{
			string event_name;
			char* context;
			strtok_s(msg, " ", &context);
			int begin_hour, begin_minute, begin_day, begin_month, begin_year, end_hour, end_minute, end_day, end_month, end_year;
			string user = strtok_s(0, " ", &context);
			event_name = strtok_s(0, " ", &context);
			begin_hour = stoi(strtok_s(0, " ", &context));
			begin_minute = stoi(strtok_s(0, " ", &context));
			begin_day = stoi(strtok_s(0, " ", &context));
			begin_month = stoi(strtok_s(0, " ", &context));
			begin_year = stoi(strtok_s(0, " ", &context));
			end_hour = stoi(strtok_s(0, " ", &context));
			end_minute = stoi(strtok_s(0, " ", &context));
			end_day = stoi(strtok_s(0, " ", &context));
			end_month = stoi(strtok_s(0, " ", &context));
			end_year = stoi(strtok_s(0, " ", &context));
			_user_[user].addevent((char*)event_name.c_str(), begin_hour, begin_minute, begin_day, begin_month, begin_year, end_hour,end_minute, end_day, end_month, end_year);
			X.Send("DONE",5);
			
		}break;
	case ShowEnd:
		{
			char* context;
			strtok_s(msg, " ", &context);
			string _user_name_ = strtok_s(0, " ", &context);
			string  event_name= strtok_s(0, " ", &context);
			X.Send(_user_[_user_name_].getendtime((char*)event_name.c_str()), 47);
			
		}break;
	case ShowStart:
		{
			
			char* context;
			strtok_s(msg, " ", &context);
			string _user_name_ = strtok_s(0, " ", &context);
			string  event_name= strtok_s(0, " ", &context);
			X.Send(_user_[_user_name_].getstarttime((char*)event_name.c_str()), 49);
		}break;
	case DeleteEvent:
		{
			char* context;
			strtok_s(msg, " ", &context);
			string _user_name_ = strtok_s(0, " ", &context);
			string  event_name= strtok_s(0, " ", &context);
			_user_[_user_name_].deleteevent(event_name);
		}break;
	case Invitepeople: 
		{
			char *context;
			strtok_s(msg, " ", &context);
			string user = strtok_s(0, " ", &context);
			string nameevent = strtok_s(0, " ", &context);
			int num = stoi(strtok_s(0, " ", &context));
			char** invite = (char**)new char[(const int)num][30];
			for(int i = 0; i<num; i++)
			{
				invite[i] = strtok_s(0, " ", &context);
			}
			_user_[user].Invitepeople((char*)nameevent.c_str(), invite, num);
			
		}break;
//----------------------Amr Shibiny-----------------End

	}
}
char* CServer::send(string temp_str)
{
	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	//X.Send(buffer,strlen(buffer)+1);
	return buffer;
}
bool CServer::IsUser(string username_)
{
	if(_user_.find(username_)==_user_.end())return false;
	return true;

}
bool CServer::check_first_time(string &ID)
{
	map<string, Account>::iterator it;
	it=_user_.find(ID);
	if(it!=_user_.end())return false;
	return true;
}
CServer::~CServer(void)
{
}